"""
Case Type   : ubtree页面解析工具
Case Name   : pagehack -t指定ubtree_pcr_index_范围分区表
Create At   : 2025-10-24
Owner       : sungang14
Description :
    1、创建ustore范围分区表
    2、创建ubtree索引
    3、插入测试数据
    4、查询索引文件路径
    5、解析索引页面
Expect      :
    1、设置成功
    2、成功
    3、成功
    4、成功
    5、解析索引页面成功
History     :
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class SlowSQLTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.userNode = Node('PrimaryDbUser')
        self.table_name = f"t_ubtree_pagehack_0005"
        self.index_name = f"i_ubtree_pagehack_0005"
        self.prepare_data_00 = f"drop table if exists {self.table_name} cascade;"
        self.prepare_data_01 = f"""create table {self.table_name} (
                                        order_id bigserial primary key,
                                        product_code varchar(20),
                                        sale_date date not null,
                                        amount numeric(10,2)
                                        ) with (storage_type=ustore)
                                        partition by range (sale_date) (
                                            partition p2025 values less than ('2026-01-01'),
                                            partition p2026 values less than ('2027-01-01'),
                                            partition p_max values less than (maxvalue)
                                    );"""
        self.prepare_data_02 = f"create index {self.index_name} on {self.table_name} using ubtree(sale_date) global with (index_type='pcr');"
        self.prepare_data_03 = f"insert into {self.table_name} (product_code, sale_date, amount) values ('p1001', '2025-03-01', 150.50), ('p1002', '2025-12-15', 200.00), ('p2001', '2026-08-15', 300.00), ('p9999', '2030-05-20', 500.00);"
        self.prepare_data_04 = f"SELECT pg_relation_filepath('{self.index_name}');"

    def test_slowsql(self):
        text = '--step1:创建ustore分区表;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_01)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, data_cmd, '执行失败' + text)

        text = '--step2:创建ubtree索引;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_02)
        logger.info(data_cmd)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, data_cmd, '执行失败' + text)

        text = '--step3:插入测试数据;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_03)
        logger.info(data_cmd)
        self.assertIn(f"INSERT 0 4", data_cmd, '执行失败' + text)

        text = '--step4:查询索引文件路径;expect:成功--'
        logger.info(text)
        time.sleep(5)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_04)
        logger.info(data_cmd)
        self.assertIn(f"1 row", data_cmd, '执行失败' + text)
        res_path = data_cmd.splitlines()[-2].strip()
        logger.info(res_path)

        text = '--step5:解析索引文件;expect:成功--'
        logger.info(text)
        ph_cmd = f"source {macro.DB_ENV_PATH}; cd $PGDATA; pagehack -f {res_path} -t ubtree_pcr_index"
        logger.info(ph_cmd)
        res = self.userNode.sh(ph_cmd).result()
        logger.info(res)
        self.assertNotIn(f'pd_checksum: 0x0, verify fail', res, '执行失败' + text)
        self.assertNotIn(self.constant.NO_FILE_MSG, res, '执行失败' + text)

    def tearDown(self):
        text = '--step6:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
